﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using MM.Business;
using MM.Entities;

namespace MM
{
    public partial class frmTimPhieuXK : Form
    {
        // the delegate the subscribers must implement
        public delegate void ChonPhieuXuatKho(object frmTimPhieuXK_KetQua, ChonPhieuXKEventArgs chonPNKevent);

        // an instance of the delegate
        public ChonPhieuXuatKho chonPXK;

        List<VAT_TU_PHU_TUNG> lstVTPT = new List<VAT_TU_PHU_TUNG>();
        frmTimKiemVTPT frm;
        frmTimKiemTheoHinh frm1;
        frmImpa_TimKiem frm2;
        frmImpa_TimKiemTheoHinh frm3;

        string _matau = "";
        string _nguoitao = "";
        string _sConn = "";

        int autoSearch = 0;

        public frmTimPhieuXK(string sConn)
        {
            InitializeComponent();
            _sConn = sConn;
            _matau = frmDangNhap.objTau._tenTau2KiTu;
            this.InitDMKho(_matau);
        }

        public frmTimPhieuXK(string sConn,CHI_TIET_PXH obj,int daduyet)
        {
            InitializeComponent();
            _sConn = sConn;
            _matau = frmDangNhap.objTau._tenTau2KiTu;
            this.InitDMKho(_matau);
            if (obj != null)
            {
                this.txtSoPhieuGI.Text = obj.MaPhieuXuatHang;
                this.lookupKho.Text = obj.MaKho;
                this.txtMaVTPT.Text = obj.MaThietBi;

                if (daduyet == 1)
                {
                    radioButton1.Checked = true;
                }
                if (daduyet == 0)
                {
                    radioButton2.Checked = true;
                }
                autoSearch = 1;
            }
            else
            {
                if (daduyet == 1)
                {
                    radioButton1.Checked = true;
                }
                if (daduyet == 0)
                {
                    radioButton2.Checked = true;
                }
                autoSearch = 1;
            }
        }

        private void InitDMKho(string matau)
        {
            clsKHO khoCtrl = new clsKHO(_sConn);
            List<KHO> lstKho = new List<KHO>();
            lstKho = khoCtrl.LoadKHOByTAU(matau);
            this.lookupKho.Properties.DataSource = lstKho;
            this.lookupKho.Properties.DisplayMember = "MaKho";
            this.lookupKho.Properties.ValueMember = "MaKho";
        }

       

        private void cmdTim_Click(object sender, EventArgs e)
        {
            int daduyet = -1;

            PHIEU_XUAT_HANG objPXH = new PHIEU_XUAT_HANG();
            objPXH.MaPhieuXuatHang = this.txtSoPhieuGI.Text;
            objPXH.MaKho = this.lookupKho.Text;
            objPXH.NguoiXuat = this.txtNguoiThucHien.Text.Trim();
            objPXH.NgayXuatFrom = this.dateEditFromDate.DateTime;
            objPXH.NgayXuatTo = this.dateEditToDate.DateTime;

            if (radioButton1.Checked == true)
            {
                daduyet = 1;
            }
            if (radioButton2.Checked == true)
            {
                daduyet = 0;
            }
            if (radioButton4.Checked == true)
            {
                objPXH.TinhTrang = 1;
            }

          

            List<PHIEU_XUAT_HANG> lst = new List<PHIEU_XUAT_HANG>();
            clsPHIEU_XUAT_HANG pxhCtrl = new clsPHIEU_XUAT_HANG(_sConn);

            if (lstVTPT.Count == 0 && this.txtMaVTPT.Text != "")
            {
                VAT_TU_PHU_TUNG vtptObj = new VAT_TU_PHU_TUNG();
                vtptObj._maVTPT = this.txtMaVTPT.Text;

                lstVTPT.Add(vtptObj);
            }

            lst = pxhCtrl.usp_SelectPHIEU_XUAT_HANGsSearch(objPXH, lstVTPT, daduyet);
            if (lst.Count > 0)
            {
                this.Hide();
                frmTimPhieuXK_KetQua f = new frmTimPhieuXK_KetQua(_sConn, lst);
                f.chonPXK += new frmTimPhieuXK_KetQua.ChonPhieuXuatKho(GetPhieuXuatKho);
                f.Location = new Point(
                    (this.Location.X + this.Width / 2 + 100) - (f.Width / 2),
                    (this.Location.Y + this.Height / 2 + 100) - (f.Height / 2));
                f.StartPosition = FormStartPosition.Manual;
                f.ShowDialog();

                this.Dispose();
            }
            else
                MessageBox.Show("KHÔNG TÌM THẤY PHIẾU XUẤT KHO THỎA ĐIỀU KIỆN");
        }

        private void GetPhieuXuatKho(object sender, ChonPhieuXKEventArgs e)
        {

            ChonPhieuXKEventArgs pxkevent = new ChonPhieuXKEventArgs(e.MaPhieuXuatKho);

            if (chonPXK != null)
            {
                chonPXK(this, pxkevent);
            }
        }

        private void frmTimPhieuXK_Paint(object sender, PaintEventArgs e)
        {
            Common.PaintGradientColor(Color.LightYellow, Color.LightSkyBlue, System.Drawing.Drawing2D.LinearGradientMode.Vertical, this.Width, this.Height, this);            
        }

        private void frmTimPhieuXK_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.F8)
            {
                this.cmdTim_Click(null, null);
            }
        }

        private void frmTimPhieuXK_Load(object sender, EventArgs e)
        {
            if (autoSearch==1)
                cmdTim_Click(null, null);
        }

        private void LuuDSVTPT(object sender, lstVtptEventArgs e)
        {
            lstVTPT = e.lstVtpt;
            if (lstVTPT.Count > 0)
            {
                this.txtMaVTPT.Text = lstVTPT[0]._maVTPT;
                this.lblTenVTPT.Text = lstVTPT[0]._tenVTPT;
            }
        }

        private void tìmTheoNhiềuTiêuChíToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (lstVTPT.Count == 0)
            {
                frm = new frmTimKiemVTPT(_sConn, true);
                frm.SelectLstMaterial += new frmTimKiemVTPT.SelectListMaterialHandler(LuuDSVTPT);
            }
            else
                lstVTPT.Clear();
            frm.ShowDialog();
        }

        private void tìmTheoHìnhToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (lstVTPT.Count == 0)
            {
                frm1 = new frmTimKiemTheoHinh(_sConn, true);
                frm1.SelectLstMaterialByImg += new frmTimKiemTheoHinh.SelectListMaterialByImgHandler(LuuDSVTPT);
            }
            else
                lstVTPT.Clear();
            frm1.ShowDialog();
        }

        private void tìmVTIMPATheoNhiềuTiêuChíToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (frm2 == null)
            {
                frm2 = new frmImpa_TimKiem(_sConn, true);
                frm2.SelectLstMaterial += new frmImpa_TimKiem.SelectListMaterialHandler(LuuDSVTPT);
            }
            else
                lstVTPT.Clear();
            frm2.ShowDialog();
        }

        private void tìmVTIMPATheoHìnhToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (frm3 == null)
            {
                frm3 = new frmImpa_TimKiemTheoHinh(_sConn, true);
                frm3.SelectLstMaterial += new frmImpa_TimKiemTheoHinh.SelectListMaterialHandler(LuuDSVTPT);
            }
            else
                lstVTPT.Clear();
            frm3.ShowDialog();
        }
    }
}